package com.orhon.smartcampus.modules.baseinfo.mutation;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.baseinfo.dao.SemesterDao;
import com.orhon.smartcampus.modules.baseinfo.entity.Semester;
import com.orhon.smartcampus.modules.baseinfo.gqlInputModel.GQLSemesterInput;
import com.orhon.smartcampus.modules.baseinfo.inputMapper.SemesterInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AddSemesterMutation implements DataFetcher {


    @Autowired
    SemesterDao semesterDao;


    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;


    /**
     * 添加学期
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);

        GQLSemesterInput input = SemesterInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Semester semester = null;

        if (input.getId() != null) {
            semester = semesterDao.get(Semester.class, input.getId());
            System.out.println("semester:"+semester);
            semester.setUpdatedAt(new Date());
        } else {
            semester = new Semester();
            semester.setCreatedAt(new Date());
        }
        if(input.getSemester_code() != null){

            semester.setSemesterCode(input.getSemester_code());
        }
        if(input.getName() != null){

            semester.setSemesterName(JSON.toJSONString(input.getName()));
        }
        if(input.getYearly_code() != null){

            semester.setYearlyCode(input.getYearly_code());
        }
        if(input.getIs_current() != null){

            semester.setIsCurrent(input.getIs_current());
        }
        if (schoolId != null) {
            semester.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class, schoolId));
        }
        if(input.getStart_date() != null){

            semester.setStartAt(input.getStart_date());
        }
        if(input.getEnd_date() != null){

            semester.setEndAt(input.getEnd_date());
        }
        if(input.getTeaching_start_date() != null){

            semester.setTeachingStartAt(input.getTeaching_start_date());
        }
        if(input.getTeaching_end_date() != null){

            semester.setTeachingEndAt(input.getTeaching_end_date());
        }
        if (input.getTotal_teaching_weeks() != null){

            semester.setWeekTotal(input.getTotal_teaching_weeks());

        }
        if(input.getSemester_status() != null){

            semester.setSemesterStatus(input.getSemester_status());
        }
        if(input.getFirst_week_monday() != null){

            semester.setFirstWeekMonday(input.getFirst_week_monday());
        }
        if(input.getFirst_week_sunday() != null){

            semester.setFirstWeekSunday(input.getFirst_week_sunday());
        }
        try {
            semesterDao.saveOrUpdate(semester);
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}

